//
//  HEDoctorServerCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/17.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEDoctorServerCell.h"
#import "HEServerDetailsInfoCell.h"
#import "HEDoctorPrvaterInfoCell.h"


@interface HEDoctorServerCell ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, assign) BOOL hasLoad;
@property (nonatomic, strong) NSArray <HEDoctorInfoModel *> *showList;

@end
 
@implementation HEDoctorServerCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    self.contentView.backgroundColor = CViewBgColor;
    
    // 1.创建流水布局
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//    _layout.estimatedItemSize = CGSizeMake(120, 100);

    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
    _collectionView.backgroundColor = CViewBgColor;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    if (@available(iOS 11.0, *)) {
        _collectionView.contentInsetAdjustmentBehavior = NO;
    } else {
        // Fallback on earlier versions
    }
    [self.contentView addSubview:_collectionView];
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(10);
        make.left.right.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView).mas_offset(-7);
    }];
    
    /// 设置此属性为yes 不满一屏幕 也能滚动
    self.collectionView.alwaysBounceHorizontal = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    [_collectionView registerClass:HEServerDetailsInfoCell.class forCellWithReuseIdentifier:KHEServerDetailsInfoCell];
    
    [_collectionView registerClass:HEDoctorPrvaterInfoCell.class forCellWithReuseIdentifier:KHEDoctorPrvaterInfoCell];
    
    
}
 

-(void)refreshContent:(GDFiterCellModel *)fiterModel formModel:(GDFormModel *)formModel indexPath:(NSIndexPath *)indexPath {
    
    _delegate = formModel.proxyVC;
    
    if (!_hasLoad) {
        _showList = formModel.doc_serverList;
        [_collectionView reloadData];
        _hasLoad = YES;
    }
}

- (CGFloat)getHeight {
    [_collectionView reloadData];
    return self.layout.collectionViewContentSize.height;
}

//- (void)getFooterViewHeightBlock:(getViewHeightBlock)block {
//    _heightBlock = block;
//}

//解决ios8上自动布局的问题 
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
    return YES;
}


#pragma mark - 代理方法 Delegate Methods
// 设置分区

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

// 每个分区上得元素个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _showList.count;
}

// 设置cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.formModel.doc_pageType == 1) {
        HEDoctorPrvaterInfoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:KHEDoctorPrvaterInfoCell forIndexPath:indexPath];
        cell.model = _showList[indexPath.row];
         return cell;
    }else {
        HEServerDetailsInfoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:KHEServerDetailsInfoCell forIndexPath:indexPath];
        cell.model = _showList[indexPath.row];
         return cell;
    }
}

// 设置cell大小 itemSize：可以给每一个cell指定不同的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
 //    CGFloat width = [self gainStringWidthWithString:self.nameArray[indexPath.row] font:15.0f height:height];
    return CGSizeMake(101, 121);
}


// 设置UIcollectionView整体的内边距（这样item不贴边显示）
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    // 上 左 下 右
    return UIEdgeInsetsMake(0, 15 , 0, 15);
}

// 设置minimumLineSpacing：cell上下之间最小的距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 15;
}

// 设置minimumInteritemSpacing：cell左右之间最小的距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 15;
}

// 选中cell的回调
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self resetChoose];
    HEDoctorInfoModel *model = _showList[indexPath.item];
    model.isChoose = YES;
    if ([_delegate respondsToSelector:@selector(chooseDoctorServerType:withModel:)]) {
        [_delegate chooseDoctorServerType:self withModel:model];
    }
    [_collectionView reloadData];
}

- (void)resetChoose {
    for (HEDoctorInfoModel *model in self.formModel.doc_serverList) {
        model.isChoose = NO;
    }
}



@end
